package com.atguigu.srb.core.service.impl;

import com.atguigu.common.exception.Assert;
import com.atguigu.common.result.ResponseEnum;
import com.atguigu.srb.core.enums.UserBindEnum;
import com.atguigu.srb.core.hfb.FormHelper;
import com.atguigu.srb.core.hfb.HfbConst;
import com.atguigu.srb.core.hfb.RequestHelper;
import com.atguigu.srb.core.mapper.UserInfoMapper;
import com.atguigu.srb.core.pojo.VO.UserBindVO;
import com.atguigu.srb.core.pojo.entity.UserBind;
import com.atguigu.srb.core.mapper.UserBindMapper;
import com.atguigu.srb.core.pojo.entity.UserInfo;
import com.atguigu.srb.core.service.UserBindService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 用户绑定表 服务实现类
 * </p>
 *
 * @author ZLHao
 * @since 2021-07-12
 */
@Service
public class UserBindServiceImpl extends ServiceImpl<UserBindMapper, UserBind> implements UserBindService {

    @Autowired
    private UserInfoMapper userInfoMapper;


    @Override
    public String bind(UserBindVO userBindVO, Long userId) {

        // 对于借款人和出资人同一人时，绑定身份证号只能是同一个
        String idCard = userBindVO.getIdCard();

        QueryWrapper<UserBind> userBindQueryWrapper = new QueryWrapper();
        userBindQueryWrapper.eq("id_card",idCard).ne("user_id",userId);
        UserBind userBind1 = baseMapper.selectOne(userBindQueryWrapper);
        Assert.isNull(userBind1, ResponseEnum.USER_BIND_IDCARD_EXIST_ERROR);

        // 获取用户在当前是否有过未完成绑定的记录，没有在本地存储一份数据
        QueryWrapper<UserBind> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId);
        UserBind userBind = baseMapper.selectOne(queryWrapper);
        if (userBind ==null){
            // 没有绑定记录，则将前台输入的数据存入本地数据一份
            userBind = new UserBind();
            // 将VO对象中与userbind同名的复制到UserBind中
            BeanUtils.copyProperties(userBindVO,userBind);
            userBind.setUserId(userId);
            userBind.setStatus(UserBindEnum.NO_BIND.getStatus());
            // 存入数据库
            baseMapper.insert(userBind);
        }else {// 如果存在之前输入 没有走完流程的，输入的数据变为更新
            BeanUtils.copyProperties(userBindVO,userBind);
            baseMapper.updateById(userBind);
        }


        // 将bindVO对象里面的字段 封装到map
        Map<String,Object> paramMap = new HashMap<>();

        paramMap.put("agentId", HfbConst.AGENT_ID); // 商户的标识
        paramMap.put("agentUserId", userId);// 用户id

        // 用户输入的信息
        paramMap.put("idCard",userBindVO.getIdCard());
        paramMap.put("personalName", userBindVO.getName());
        paramMap.put("bankType", userBindVO.getBankType());
        paramMap.put("bankNo", userBindVO.getBankNo());
        paramMap.put("mobile", userBindVO.getMobile());

        // 回调的url   异步  和同步
        paramMap.put("returnUrl", HfbConst.USERBIND_RETURN_URL);
        paramMap.put("notifyUrl", HfbConst.USERBIND_NOTIFY_URL);
        paramMap.put("timestamp", RequestHelper.getTimestamp());
        // 将sign加密后传出
        paramMap.put("sign", RequestHelper.getSign(paramMap));

        // 将数据填入表单返回给前台
        String buildForm = FormHelper.buildForm(HfbConst.USERBIND_URL, paramMap);
        return buildForm;
    }

    @Override
    @Transactional
    public void notifyMethod(Map<String, Object> paramMap) {

        // 向user_bind  和 user_info 更新字段
        // 获取字段
        String bindCode =(String) paramMap.get("bindCode");
        String agentUserId =(String) paramMap.get("agentUserId");

        QueryWrapper<UserBind> queryWrapper = new QueryWrapper();
        queryWrapper.eq("user_id",agentUserId);

        // 先获取用户
        // 更新绑定表
        UserBind userBind = baseMapper.selectOne(queryWrapper);
        userBind.setBindCode(bindCode);
        userBind.setStatus(UserBindEnum.BIND_OK.getStatus());
        baseMapper.updateById(userBind);

        // 更新用户信息表
        UserInfo userInfo = userInfoMapper.selectById(agentUserId);
        userInfo.setBindCode(bindCode);
        userInfo.setBindStatus(UserBindEnum.BIND_OK.getStatus());
        userInfo.setName(userBind.getName());
        userInfo.setIdCard(userBind.getIdCard());
        userInfoMapper.updateById(userInfo);

    }

    @Override
    public String getBindCodeByUserId(Long userId) {
        QueryWrapper<UserBind> userBindQueryWrapper = new QueryWrapper<>();
        userBindQueryWrapper.eq("user_id", userId);
        UserBind userBind = baseMapper.selectOne(userBindQueryWrapper);
        String bindCode = userBind.getBindCode();
        return bindCode;

    }
}
